<?php

namespace App\Http\Controllers\Api;

use App\Models\Area;
use App\Models\UserAddress;
use Illuminate\Http\Request;

class UserAddressController extends BaseController
{
    public function areas(Request $request)
    {
        $type = intval($request->get('type',1));
        $pid = intval($request->get('pid',0));

        if(empty($type) || !in_array($type,[1,2,3]))
        {
            return $this->err(1,200,'','参数错误');
        }

        if($type == 1)
        {
            $areaModel = new Area();
            $list = $areaModel->getProvinces();
        }
        else
        {
            $list = Area::query()->select(['id','name'])->where('pid',$pid)->where('type',$type)->get();
        }



        $items = [];
        if(!empty($list))
        {
            foreach ($list as $v)
            {
                $items[] = [
                    'id'    => $v->id,
                    'name'  => $v->name,
                ];
            }
        }

        $data = [
            'items' => $items
        ];
        return $this->res([
            'data' => $data
        ]);
    }

    public function index(Request $request)
    {
        $page      = intval($request->get('page',1));
        $page      = max($page ,1);
        $page_size = intval($request->get('page_size',10));

        $builder = UserAddress::query()->where('deleted',0)->where('user_id',$this->user_id);

        $columns = ['id','receiver','mobile','province_name','city_name','area_name','address'];
        $list = $builder->orderBy('id','desc')->paginate($page_size,$columns,'page',$page);

        $items = [];
        if(!empty($list->items()))
        {
            foreach ($list->items() as $item)
            {
                $items[] = [
                    'id'             => $item->id,
                    'receiver'       => $item->receiver,
                    'mobile'         => $item->mobile,
                    'province_name'  => $item->province_name,
                    'city_name'      => $item->city_name,
                    'area_name'      => $item->area_name,
                    'address'        => $item->address,
                ];
            }
        }

        $data = [
            '_meta' => [
                'total_count'  => $list->total(),
                'page_count'   => $list->lastPage(),
                'current_page' => $list->currentPage(),
                'per_page'     => $list->perPage()
            ],
            "items" => $items
        ];

        return $this->res([
            'data' => $data
        ]);
    }
    public function store(Request $request)
    {
        $receiver      = trim($request->get('receiver'));
        $mobile        = trim($request->get('mobile'));
        $province_id   = intval($request->get('province_id'));
        $province_name = trim($request->get('province_name'));
        $city_id       = intval($request->get('city_id'));
        $city_name     = trim($request->get('city_name'));
        $area_id       = intval($request->get('area_id'));
        $area_name     = trim($request->get('area_name'));
        $address       = trim($request->get('address'));

        if(empty($receiver) || empty($mobile) || empty($address) || empty($province_name) || empty($province_id) || empty($city_id) || empty($city_name) || empty($area_id) || empty($area_name))
        {
            return $this->err(1,200,'','参数错误');
        }

        $data = [
            'user_id'       => $this->user_id,
            'receiver'      => $receiver,
            'mobile'        => $mobile,
            'province_id'   => $province_id,
            'province_name' => $province_name,
            'city_id'       => $city_id,
            'city_name'     => $city_name,
            'area_id'       => $area_id,
            'area_name'     => $area_name,
            'address'       => $address,
            'created_at'    => $this->time,
            'updated_at'    => $this->time,
        ];
        $address = UserAddress::create($data);
        return $this->ok(['address_id' => $address->id],'添加成功');
    }

    public function update(Request $request)
    {
        $id            = intval($request->get('id'));
        $receiver      = trim($request->get('receiver'));
        $mobile        = trim($request->get('mobile'));
        $province_id   = intval($request->get('province_id'));
        $province_name = trim($request->get('province_name'));
        $city_id       = intval($request->get('city_id'));
        $city_name     = trim($request->get('city_name'));
        $area_id       = intval($request->get('area_id'));
        $area_name     = trim($request->get('area_name'));
        $address       = trim($request->get('address'));

        if(empty($id) || empty($receiver) || empty($mobile) || empty($address) || empty($province_name) || empty($province_id) || empty($city_id) || empty($city_name) || empty($area_id) || empty($area_name))
        {
            return $this->err(1,200,'','参数错误');
        }
        $userAddress = $this->_check_info($id);
        if(empty($userAddress) || $userAddress->user_id != $this->user_id)
        {
            return $this->err(1,200,'','收货地址不存在');
        }

        $data = [
            'user_id'       => $this->user_id,
            'receiver'      => $receiver,
            'mobile'        => $mobile,
            'province_id'   => $province_id,
            'province_name' => $province_name,
            'city_id'       => $city_id,
            'city_name'     => $city_name,
            'area_id'       => $area_id,
            'area_name'     => $area_name,
            'address'       => $address,
            'updated_at'    => $this->time,
        ];
        $address = $userAddress->update($data);
        return $this->ok([],'修改成功');
    }

    private function _check_info($id)
    {
        $userAddress = UserAddress::query()->where('id',$id)->where('deleted',0)->first();
        return $userAddress;
    }

    public function show(Request $request)
    {
        $id = intval($request->get('id'));
        $userAddress = $this->_check_info($id);
        if(empty($userAddress) || $userAddress->user_id != $this->user_id)
        {
            return $this->err(1,200,'','收货地址不存在');
        }

        $data = [
            'id'            => $userAddress->id,
            'receiver'      => $userAddress->receiver,
            'mobile'        => $userAddress->mobile,
            'province_id'   => $userAddress->province_id,
            'province_name' => $userAddress->province_name,
            'city_id'       => $userAddress->city_id,
            'city_name'     => $userAddress->city_name,
            'area_id'       => $userAddress->area_id,
            'area_name'     => $userAddress->area_name,
            'address'       => $userAddress->address,
        ];
        return $this->ok($data);
    }

    public function destroy(Request $request)
    {
        $id = intval($request->get('id'));
        if(empty($id))
        {
            return $this->err(1,200,'','参数错误');
        }

        $userAddress = $this->_check_info($id);
        if(empty($userAddress) || $userAddress->user_id != $this->user_id)
        {
            return $this->err(1,200,'','收货地址不存在');
        }

        if(!$userAddress->deleted)
        {
            $userAddress->update([
                'deleted'      => 1,
                'deleted_at'   => $this->time
            ]);
            return $this->ok([],'删除成功');
        }

        return $this->err(1,200,'','删除失败');
    }
}
